import { Component, OnInit } from '@angular/core';
import { QueryService } from "./query.service";
import { InvestService } from "../../invest.service";
import { Util } from '../../../../../common/util';
import * as _ from 'lodash';

@Component({
  selector: 'app-query',
  templateUrl: './query.component.html',
  styleUrls: ['./query.component.scss'],
  providers: [QueryService]
})
export class QueryComponent implements OnInit {
  // 用于识别指标类型
  public type: any;
  // 用于保存基金代码显示值
  public sFundCodeZh: any = [];
  // 查询body
  public queryBody: any = {
    pageIndex: 1,
    pageSize: 50,
    nFundNo: [],
    sFundType: '',
    sStatus: [],
    f1: '',
    f2: '',
    f3: '',
    f4: '',
    f6: '',
    f7: '',
    f8: '',
    f9: '',
    f10: '',
    f12: '',
    f13: '',
    f14: '',
    f15: '',
    f16: '',
    f17: '',
    f18: '',
    f19: '',
    f20: '',
    f21: '',
    f22: '',
    f23: '',
    f24: '',
    f25: '',
    f26: '',
    f27: '',
    f28: '',
    f29: '',
    f30: '',
    f31: '',
    f32: '',
    f33: '',
    f34: '',
    f35: '',
    f36: '',
    f37: '',
    f38: '',
    f41: '',
    f42: '',
    f43: '',
    f44: '',
    f45: '',
    f46: '',
    f47: '',
    f48: '',
    f49: '',
    f50: '',
    f51: '',
    f52: '',
    f53: '',
    f54: '',
    f55: '',
    f56: '',
    f57: '',
    f58: ''
  };
  // 基金名称
  public sFundName: any;
  // 基金类型列表
  public fundTypeInfo: any;
  // 基金类型分类列表
  public fundTypeList: any;
  // 搜索结果列表
  public searchList: any;
  // 基金类型全选
  public typeCheckedAll: any = false;
  // 搜索结果全选
  public searchCheckedAll: any = false;
  // 用于控制搜索结果是否显示
  public isShowSearch: any = false;
  // 基金状态下拉框值
  public fundList: any = [];
  // 字典值
  public dictionaryList: any = {
    FUND_TYPE: [],
    ID_FIELD_TYPE: []
  };

  constructor(
    public investService: InvestService,
    public queryService: QueryService
  ) { }

  ngOnInit() {
    // 获取基金代码显示值
    // this.sFundCodeZh = this.investService.sFundCodeZh;
    // 获取查询条件
    // this.queryBody = this.investService.queryBody;
    // 获取基金状态
    this.fundList = JSON.parse(localStorage.getItem("fundList"));
    // 字典值
    this.dictionaryList = JSON.parse(localStorage.getItem("dictionaryList"));
  }
  ngAfterViewInit() {
    // 基金状态下拉框赋值
    this.setFundStatus(this.queryBody.sFundType);
  }
  // 基金状态下拉框赋值
  setFundStatus(fundType: any) {
    let that = this;
    if (fundType == '') {
      that.queryBody.sStatus = [];
      window['$']('#queryFundStatus').selectpicker('destroy');
      Util.loadSelect([], 'queryFundStatus');
    } else {
      _.forEach(this.fundList, item => {
        if (item.sFundType == fundType) {
          setTimeout(function () {
            window['$']('#queryFundStatus').selectpicker('destroy');
            Util.loadSelect(item.subTree, 'queryFundStatus');
            window['$']('#queryFundStatus').selectpicker('val', that.queryBody.sStatus);
          }, 1);
        }
      })
    }
  }
  // 查询
  query() {
    // this.investService.sFundCodeZh = this.sFundCodeZh;
    this.queryBody['isSelect'] = 1;
    this.investService.queryBody = this.queryBody;
    this.investService.query.emit(this.type);
    window['$']('#query').modal('hide');
  }
  // 弹出基金列表
  showFund() {
    this.sFundName = '';
    this.isShowSearch = false;
    this.searchCheckedAll = false;
    this.searchList = [];
    this.listByFundType();
    window['$']('#fund').modal('show');
  }
  // 全选
  checkedAll(data, checkedAll) {
    if (checkedAll) {
      _.forEach(data, item => {
        item.isChecked = true;
      });
    } else {
      _.forEach(data, item => {
        item.isChecked = false;
      });
    }
  }
  // 全选控制
  ctrlCheckAll(data, checkAllBtn) {
    let that = this;
    let checkList = [];
    _.forEach(data, item => {
      if (item.isChecked == true) {
        checkList.push(item);
      }
    })
    if (checkList.length == data.length) {
      this.checkedAllType(checkAllBtn, true);
    } else {
      this.checkedAllType(checkAllBtn, false);
    }
  }
  // 全选按钮赋值
  checkedAllType(checkAllBtn, isTrue) {
    switch (checkAllBtn) {
      // 搜索结果全选按钮
      case 'typeCheckedAll':
        this.typeCheckedAll = isTrue;
        break;
      // 搜索结果全选按钮
      case 'searchCheckedAll':
        this.searchCheckedAll = isTrue;
        break;
    }
  }
  // 获取基金类型列表
  listByFundType() {
    let that = this;
    let getBody = {
      sType: 0
    }
    this.queryService.listByFundType(getBody).subscribe(data => {
      if (data) {
        that.fundTypeInfo = data;
        that.selectType('公募基金');
      }
    })
  }
  // 获取基金类型分类列表
  selectType(type: any) {
    let that = this;
    _.forIn(this.fundTypeInfo, (value, key) => {
      if (type == key) {
        that.fundTypeList = value;
        that.ctrlCheckAll(that.fundTypeList, 'typeCheckedAll');
      }
    })
  }
  // 获取搜索结果
  searchist() {
    let that = this;
    let getBody = {
      sFundName: this.sFundName,
      sType: 0
    }
    this.queryService.searchList(getBody).subscribe(data => {
      if (data) {
        that.searchList = data;
        that.isShowSearch = true;
      }
    })
  }
  // 保存
  saveSelect() {
    let that = this;
    this.sFundCodeZh = [];
    let checkItem = [];
    _.forIn(this.fundTypeInfo, (value, key) => {
      _.forEach(value, item => {
        if (item.isChecked) {
          checkItem.push(item.nFundNo);
          // 保存显示值
          if (item.sFundCode == '' || item.sFundCode == null) {
            that.sFundCodeZh.push(item.nFundNo);
          } else {
            that.sFundCodeZh.push(item.sFundCode);
          }
        }
      })
    })
    _.forEach(this.searchList, item => {
      if (item.isChecked) {
        checkItem.push(item.nFundNo);
        // 保存显示值
        if (item.sFundCode == '' || item.sFundCode == null) {
          that.sFundCodeZh.push(item.nFundNo);
        } else {
          that.sFundCodeZh.push(item.sFundCode);
        }
      }
    })
    this.queryBody.nFundNo = _.uniq(checkItem);
    // 保存显示值
    this.sFundCodeZh = _.uniq(this.sFundCodeZh).join(",");
    window['$']('#fund').modal('hide');
  }
  // 关闭模态框
  closeModel() {
    window['$']('#fund').modal('hide');
  }
  // 重置查询条件
  reset() {
    this.queryBody = {
      pageIndex: 1,
      pageSize: 50,
      nFundNo: [],
      sFundType: '',
      sStatus: [],
      f1: '',
      f2: '',
      f3: '',
      f4: '',
      f6: '',
      f7: '',
      f8: '',
      f9: '',
      f10: '',
      f12: '',
      f13: '',
      f14: '',
      f15: '',
      f16: '',
      f17: '',
      f18: '',
      f19: '',
      f20: '',
      f21: '',
      f22: '',
      f23: '',
      f24: '',
      f25: '',
      f26: '',
      f27: '',
      f28: '',
      f29: '',
      f30: '',
      f31: '',
      f32: '',
      f33: '',
      f34: '',
      f35: '',
      f36: '',
      f37: '',
      f38: '',
      f41: '',
      f42: '',
      f43: '',
      f44: '',
      f45: '',
      f46: '',
      f47: '',
      f48: '',
      f49: '',
      f50: '',
      f51: '',
      f52: '',
      f53: '',
      f54: '',
      f55: '',
      f56: '',
      f57: '',
      f58: ''
    };
    window['$']('#queryFundStatus').selectpicker('val', []);
    this.investService.queryBody = this.queryBody;
    this.sFundCodeZh = [];
    this.investService.sFundCodeZh = [];
  }
}
